package com.glinsoft.ticktak.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import com.glinsoft.ticktak.entity.Cluster;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import lombok.Data;

import org.springframework.beans.BeanUtils;

import java.io.Serializable;

import java.time.LocalDateTime;

import java.util.ArrayList;
import java.util.List;


/**
 * cluster
 */
@SuppressWarnings("serial")
@ApiModel("集群配置")
@Data
public class ClusterVo implements Serializable {
    /** 主键； id **/
    @ApiModelProperty(value = "主键")
    public String id;

    /** 多实例部署； multi_instance **/
    @ApiModelProperty(value = "多实例部署")
    public Integer multiInstance;

    /**  paxos **/
    @ApiModelProperty(value = "")
    public Integer paxos;

    /** 当前节点是主节点;0:否 ,1:是； main **/
    @ApiModelProperty(value = "当前节点是主节点;0:否 ,1:是")
    public Integer main;

    /** PAXOS同步间隔;毫秒； heartbeat **/
    @ApiModelProperty(value = "PAXOS同步间隔;毫秒")
    public Integer heartbeat;

    /** 使用分布式锁； cluster_lock **/
    @ApiModelProperty(value = "使用分布式锁")
    public Integer clusterLock;

    /** 同步服务器(Redis)地址； cluster_sync_server **/
    @ApiModelProperty(value = "同步服务器(Redis)地址")
    public String clusterSyncServer;

    /** 同步服务器端口号； cluster_sync_port **/
    @ApiModelProperty(value = "同步服务器端口号")
    public Integer clusterSyncPort;

    /** 同步服务器类型;0:Redis,1:ZooKeeper,3:Consul,4:Other； cluster_sync_type **/
    @ApiModelProperty(value = "同步服务器类型;0:Redis,1:ZooKeeper,3:Consul,4:Other")
    public Integer clusterSyncType;

    /** 其他任务调度服务实例的,地址:端口号;使用半角分号连接； other_servers **/
    @ApiModelProperty(value = "其他任务调度服务实例的,地址:端口号;使用半角分号连接")
    public String otherServers;

    /** 自动刷新配置;0:否,1:是； auto_refresh_config **/
    @ApiModelProperty(value = "自动刷新配置;0:否,1:是")
    public Integer autoRefreshConfig;

    /** 创建时间； create_time **/
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建时间")
    public LocalDateTime createTime;

    public static ClusterVo fromPO(Cluster po) {
        if (po == null) {
            return null;
        }

        ClusterVo vo = new ClusterVo();
        BeanUtils.copyProperties(po, vo);

        return vo;
    }

    public static List<ClusterVo> fromPO(List<Cluster> pos) {
        if ((pos == null) || (pos.size() == 0)) {
            return null;
        }

        List<ClusterVo> r = new ArrayList();

        for (Cluster po : pos) {
            r.add(fromPO(po));
        }

        return r;
    }

    public Cluster toPO() {
        Cluster po = new Cluster();
        BeanUtils.copyProperties(this, po);

        return po;
    }

    public static List<Cluster> toPO(List<ClusterVo> vos) {
        if ((vos == null) || (vos.size() == 0)) {
            return null;
        }

        List<Cluster> r = new ArrayList();

        for (ClusterVo vo : vos) {
            r.add(vo.toPO());
        }

        return r;
    }
}
